﻿using System;
using System.Linq;
using System.Web;
using System.Web.Http;
using Medora.Model.Institutions;
using Medora.Services.Institutions;
using Microsoft.AspNet.Identity;

namespace MvcApp.Controllers
{
    [RoutePrefix("api/Scheduling")]
    public class SchedulingController : BaseApiController
    {
        private readonly IScheduleItemsService _scheduleItemsService;

        public SchedulingController(IScheduleItemsService scheduleItemsService)
        {
            _scheduleItemsService = scheduleItemsService;
        }

        [Route("GetByDate")]
        public IHttpActionResult GetByDate(string startParam, string endParam, Guid? id)
        {
            try
            {
                DateTime start = Convert.ToDateTime(startParam); // DateTime.ParseExact(startParam, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                DateTime end = Convert.ToDateTime(endParam); //DateTime.ParseExact(endParam, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                var items =
                     _scheduleItemsService.Get(new ScheduleItemFilter
                     {
                         Deleted = false,
                         StartDateTimeFrom =start,
                         StartDateTimeTo = end,
                         ProviderId = id
                     }).OrderBy(o => o.StartDateTime);
                 var events = items.Select(
                     o =>
                         new Event
                         {
                             Id = o.Id,
                             Title = (o.ScheduleActivity!=null)? o.ScheduleActivity.Person.LastName: string.Empty,
                             Start = o.StartDateTime,
                             End = o.StartDateTime.AddMinutes(o.Length),
                             AllDay = false,
                             BackgroundColor = o.ScheduleType.Color,
                             BorderColor = o.ScheduleType.Color,
                             ScheduleTypeId=o.ScheduleTypeId,
                             TextColor="#000"
                         }).ToList();

                 return Ok(events);
               

            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

        [Route("GetByPerson")]
        public IHttpActionResult GetByPerson(Guid personId)
        {
            try
            {
               // DateTime start = Convert.ToDateTime(startParam); // DateTime.ParseExact(startParam, "dd-MM-yyyy", CultureInfo.InvariantCulture);
               // DateTime end = Convert.ToDateTime(endParam); //DateTime.ParseExact(endParam, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                var items =
                     _scheduleItemsService.GetWithProvider(new ScheduleItemFilter
                     {
                         Deleted = false,
                    //     StartDateTimeFrom = start,
                     //    StartDateTimeTo = end,
                         PersonId = personId
                     }).OrderBy(o => o.StartDateTime);
                var events = items.Select(
                    o =>
                        new 
                        {
                            Id = o.Id,
                            Start = o.StartDateTime,
                            End = o.StartDateTime.AddMinutes(o.Length),
                            Provider=o.Provider.FullName,
                            Room=o.ScheduleActivity.Room,
                            Referral=o.ScheduleActivity.Referral
                        }).ToList();

                return Ok(events);


            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

        [HttpPost]
        public IHttpActionResult Post([FromBody] dynamic value)
        {
            try
            {
                Guid personId = new Guid(value.personId.Value);
                Guid id = new Guid(value.id.Value);
                _scheduleItemsService.WriteActivity(id, personId, GetCurrentUserId().Value);
                return Ok("Person scheduled");
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

        [Route("DeleteActivity")]
        [HttpPost]
        public IHttpActionResult DeleteActivity(Guid itemId, string reason)
        {
            var userId = GetCurrentUserId().Value;
            _scheduleItemsService.ClearActivity(itemId, userId, reason);
            return Ok("Successfully deleted");
        }
    }

    

    public class Event
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public string Class { get; set; }
        public bool AllDay { get; set; }
        public string BackgroundColor{ get; set; }
        public string BorderColor { get; set; }
        public string TextColor { get; set; }
        public Guid ScheduleTypeId { get; set; }
    }
}